<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>


<style>
    abbr{border-bottom: none !important;; cursor:pointer !important;}
</style>
<div el="gis" gisSrc="${gisResourcePath}" style="display:none"></div>

<input id="JQ_beginMileage" value="" type="hidden">
<input id="JQ_endMileage" value="" type="hidden">
<form id="JQ_form">
<div class="zy-input-group w-32 w-new">
    <div class="zy-input-meta"><i class="zy-requird-icon"></i>管道名称</div>
    <div class="zy-select-inner">
        <blockquote id="JQ_pipeline" class="bk-select zy-verify" dvalue="${lineId}" data-verify="{required:true,title:'管道名称'}"></blockquote>
    </div>
</div>
<div class="zy-sp2"></div>
<div class="zy-input-group w-32 w-new">
    <div class="zy-input-meta"><i class="zy-requird-icon"></i>检测任务</div>
    <div class="zy-select-inner">
        <blockquote id="JQ_task" class="bk-select zy-verify" dvalue="${taskId}" data-verify="{required:true,title:'检测任务'}"></blockquote>
    </div>
</div>

<div class="zy-sp2"></div>
<div class="zy-input-group w-32 w-new">
    <div style="width:48%;float:left">
        <div class="zy-input-meta">起点位置</div>
        <div class="zy-select-inner">
            <blockquote id="JQ_begin" class="bk-select" dvalue="${beginMileage}" ></blockquote>
        </div>
    </div>
    <div class="zy-sp4"></div>
    <div style="width:48%;float:left">
        <div class="zy-input-meta">终点位置</div>
        <div class="zy-select-inner">
            <blockquote id="JQ_end" class="bk-select"  dvalue="${endMileage}" ></blockquote>
        </div>
    </div>
    <div class="clear"></div>

</div>
</form>


<script type="text/javascript">


    $(function(){
        var lineId = window.sessionStorage.getItem("lineId");
        var taskId = window.sessionStorage.getItem("taskId");
        var beginMileage = window.sessionStorage.getItem("beginMileage");
        var endMileage = window.sessionStorage.getItem("endMileage");
        var beginMileageName = window.sessionStorage.getItem("beginMileageName");
        var endMileageName = window.sessionStorage.getItem("endMileageName");

        <c:if test="${not empty lineId}">
            lineId = "${lineId}";
            taskId = "${taskId}";
            beginMileage = "${beginMileage}";
            endMileage = "${endMileage}";
            beginMileageName = "${beginMileageName}";
            endMileageName = "${endMileageName}";
        </c:if>

        if(lineId !=undefined )
        {
            $("#JQ_pipeline").attr("dvalue",lineId);
            $("#JQ_task").attr("dvalue",taskId);
            $("#JQ_begin").attr("dvalue",beginMileage);
            $("#JQ_end").attr("dvalue",endMileage);
        }

        //管道
        var $pipeline = $('#JQ_pipeline').select3();
        window.line=$pipeline;
        // 起点位置
        var $begin = $('#JQ_begin').select3({
            title: '起点位置',
            server: true,
            mileType: 'float',
            param: {'pipeLineId': $pipeline,'beginMileage':$('#JQ_beginMileage'), 'endMileage':$('#JQ_endMileage')}
        });
        window.begin=$begin;
        // 终点位置
        var $end = $('#JQ_end').select3({
            title: '终点位置',
            server: true,
            mileType: 'float',
            param: {'pipeLineId': $pipeline, 'beginSignId': $begin, 'beginMileage':$('#JQ_beginMileage'), 'endMileage':$('#JQ_endMileage')}
        });
        window.end=$end;

        /**
         * 初始化起始位置
         */
        if(beginMileage !=undefined ) {
            window.begin.add([{id: beginMileage, mile: beginMileage, text: beginMileageName}]).fit(beginMileage);
            window.end.add([{id: endMileage, mile: endMileage, text: endMileageName}]).fit(endMileage);
        }



        // 检测任务
        var $task = $('#JQ_task').select3({
            search:false,
            title: '起点位置',
            server: true,
            usable: false,
            param: {'pipeLineId':$pipeline.val()}
        });
        window.task=$task;
        //填充起止位置
        $task.element.on('select',function(e,v){
            if(v){
                var row=getRow(v,$('#JQ_task').data("data"));
                window.begin.add([{id:row.BEGINMILEAGE,mile:row.BEGINMILEAGE,text:row.BEGINNAME}]).fit(row.BEGINMILEAGE);
                window.end.add([{id:row.ENDMILEAGE,mile:row.ENDMILEAGE,text:row.ENDNAME}]).fit(row.ENDMILEAGE);

//                $('#JQ_beginMileage').val(row.BEGINMILEAGE);
//                $('#JQ_endMileage').val(row.ENDMILEAGE);

            }else{
                window.begin.add([],false);
                window.end.add([],false);
            }
        });






        //填充管道
        $pipeline.element.on('select',function(e,v){
            if(v){
                DyyBase.ajax(path+"/comm/taskByLineId.json", {lineId:v}, function(dataset){
                    var json = getSelect(dataset);
                    window.task.add(json);

                    $('#JQ_task').data("data",dataset.result);
                    var begin = DyyBase.getPos(dataset, 'begin');
                    var end = DyyBase.getPos(dataset, 'end');
                    $begin.asset(begin);
                    $end.asset(end);
                });
//
            }else{
                window.task.add([],false);
                window.begin.add([],false);
                window.end.add([],false);
            }
        });
//







        if($pipeline.val() != "" )
        {
            var lineId=$pipeline.val();
            if($("#JQ_pipeline").attr("dvalue") != "")
            {
                lineId=$("#JQ_pipeline").attr("dvalue");
                $pipeline.fit(lineId);
            }

            DyyBase.ajax(path+"/comm/taskByLineId.json", {lineId:lineId}, function(dataset){
                var json = getSelect(dataset);

                if(json.length == 0)
                {
                    return ;
                }
                if($("#JQ_task").attr("dvalue") != "")
                {
                    window.task.add(json).fit($("#JQ_task").attr("dvalue"));

                }else{
                    window.task.add(json).fit(json[0].id);

                }

                $('#JQ_task').data("data",dataset.result);

                if($("#JQ_begin").attr("dvalue") != "")
                {

                    window.begin.add([{id:beginMileage,mile:beginMileage,text:beginMileageName}]).fit(beginMileage);
                    window.end.add([{id:endMileage,mile:endMileage,text:endMileageName}]).fit(endMileage);
                    if(typeof (submitForm)=="function")
                    {
                        submitForm(1,10);
                    }

                }else{

                    var row=getRow(window.task.val(),$('#JQ_task').data("data"));
                    window.begin.add([{id:row.BEGINMILEAGE,mile:row.BEGINMILEAGE,text:row.BEGINNAME}]).fit(row.BEGINMILEAGE);
                    window.end.add([{id:row.ENDMILEAGE,mile:row.ENDMILEAGE,text:row.ENDNAME}]).fit(row.ENDMILEAGE);
                }


            });
        }


        <c:if test="${!empty param.endMileage }" >
            window.begin.add([{id:beginMileage,mile:beginMileage,text:beginMileageName}]).fit(beginMileage);
            window.end.add([{id:endMileage,mile:endMileage,text:endMileageName}]).fit(endMileage);
            window.task.fit(taskId);

        </c:if>

    });

    //根据id 获取row
    function getRow(id,data)
    {

        for(var i=0;i<data.length;i++)
        {
            if(data[i].ID == id)
            {
                return data[i];
            }
        }
        return null;
    }


    function getSelect(d,t){
        var arr = [];
        var len = d.result.length;
        if(len > 0){
            for(var i = 0; i < len; i++){
                var obj = {};
                if(t){
                    if(d.result[i].id && d.result[i].mileage){
                        obj.id = d.result[i].id;
                        obj.text = d.result[i].mileage;
                        arr.push(obj);
                    }
                }else{
                    if(d.result[i].ID && d.result[i].NAME){
                        obj.id = d.result[i].ID;
                        obj.text = d.result[i].NAME;
                        arr.push(obj);
                    }
                }
            }
        }
        return arr;
    }

</script>